import Vue from 'vue'
import store from './store'
import App from './App'
import * as filters from './filters'
import * as config from './config'
Object.keys(filters).forEach(key => {
	Vue.filter(key, filters[key])
})
const defConfig = config.def
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	})
}
let userInfo = undefined
const logout = () => {
	userInfo = undefined
	uni.removeStorage({
		key: 'userInfo'
	})
}
const setUserInfo = (i) => {
	userInfo = i
}
const isAuth = () => {
	return userInfo && ((userInfo.userType == 0) || (userInfo.userType == 1))
}
let loginLock = false
const request = (api, data = {}, header = {}) => {
	//异步请求数据
	return new Promise(resolve => {
		if (!userInfo || !userInfo.token) {
			userInfo = uni.getStorageSync('userInfo')
		}
		let baseUrl = config.def().baseUrl
		let token = userInfo.token || ''
		// let token = '949196e467fa4a09b20b297bae4308ff'
		// let token = 'ba02ac1ae8154461bb8ab87968d0558e'
		let content = header.content || 'application/json'
		let method = header.method || 'POST'
		let failback = header.failback || null
		uni.request({
			url: baseUrl + api,
			data: {
				...data
			},
			method: method,
			header: {
				'Content-Type': content,
				'token': token
			},
			success: (res) => {
				console.log(res.data)
				if (res.statusCode === 200 && (res.data.code === 200 || res.data.token)) {
					resolve(res.data)
				} else {
					if (res.data.code === 1001) {
						if (!loginLock) {
							loginLock = true
							uni.showModal({
								title: '登录提示',
								content: '您尚未登录，是否立即登录?',
								showCancel: true,
								confirmText: '登录',
								success: (e) => {
									if (e.confirm) {
										uni.navigateTo({
											url: '/pages/public/login'
										})
									}
								},
								fail: () => {},
								complete: () => {
									loginLock = false
								}
							})
						}
					} else {
						if (failback) {
							failback(res.data)
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				}
			},
			complete: (res) => {}
		})
	})
}

function get_suffix(filename) {
	var pos = filename.lastIndexOf('.')
	var suffix = ''
	if (pos != -1) {
		suffix = filename.substring(pos)
	}
	return suffix
}

function random_string(len) {
	len = len || 32
	var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
	var maxPos = chars.length
	var pwd = ''
	for (var i = 0; i < len; i++) {
		pwd += chars.charAt(Math.floor(Math.random() * maxPos))
	}
	return pwd
}
const prePage = () => {
	let pages = getCurrentPages()
	let prePage = pages[pages.length - 2]
	// #ifdef H5
	return prePage
	// #endif
	return prePage.$vm
}
const color16 = () => { //十六进制颜色随机
	var r = Math.floor(Math.random() * 256);
	var g = Math.floor(Math.random() * 256);
	var b = Math.floor(Math.random() * 256);
	var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
	return color;
}
const sliceTime = (time) => { //处理时间 返回 06-30 10:40
	return time && time.slice(5, 16).replace('T', ' ')
}
const globalData = {}
Vue.config.productionTip = false
Vue.prototype.$fire = new Vue()
Vue.prototype.$store = store
Vue.prototype.$api = {
	msg,
	prePage,
	request,
	logout,
	isAuth,
	setUserInfo,
	defConfig,
	globalData
}
Vue.prototype.$util = {
	color16,
	sliceTime
}
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
